//
//  NSString+Number.m
//  SleepingElf
//
//  Created by karong on 2023/6/16.

#import "NSString+Number.h"

@implementation NSString (Number)

+ (NSString *)gr10Thousands1DecimalWithValue:(NSInteger)vaule {
    if (vaule >= 10000) {
        return [NSString stringWithFormat:@"%.1fw", vaule * 1.0 / 10000];
    } else {
        NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
        moneyFormatter.roundingMode = kCFNumberFormatterRoundDown;
        moneyFormatter.positiveFormat = @"###,###";
        return [moneyFormatter stringFromNumber:@(vaule)];
    }
    return @"";
}

+ (NSString *)grThousandsWithValue:(CGFloat)vaule {
    NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
    moneyFormatter.roundingMode = kCFNumberFormatterRoundDown;
    vaule = vaule <=0 ? 0 : vaule;
    if (vaule >= 10000) {
        moneyFormatter.positiveFormat = @"###,###,###k";
        return [moneyFormatter stringFromNumber:@(vaule * 1.0 / 1000)];
    }
    else {
        moneyFormatter.positiveFormat = @"###,###";
        return [moneyFormatter stringFromNumber:@(vaule)];
    }
}

+ (NSString *)grSave1DecimalWithValue:(CGFloat)vaule {
    NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
    moneyFormatter.positiveFormat = @"###,###.#";
    moneyFormatter.roundingMode = kCFNumberFormatterRoundDown;
    return [moneyFormatter stringFromNumber:@(vaule)];
}

+ (NSString *)grSave2DecimalWithValue:(CGFloat)vaule {
    NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
    moneyFormatter.positiveFormat = @"###,###.##";
    moneyFormatter.roundingMode = kCFNumberFormatterRoundDown;
    return [moneyFormatter stringFromNumber:@(vaule)];
}


+ (NSString *)grFormatFloat:(float)f {
    if (fmodf(f, 1)==0) {
        //小數點后為0
        return [NSString stringWithFormat:@"%.0f",f];
    } else {
        return [NSString stringWithFormat:@"%.1f",f];
    }
}
@end
